package com.ruoyi.system.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.domain.parm.BusinessLoginParm;
import com.ruoyi.system.domain.parm.BusinessPasswordParm;
import com.ruoyi.system.util.GlobalConstants;
import com.ruoyi.system.util.TokenUtil;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysBusinessMapper;
import com.ruoyi.system.domain.SysBusiness;
import com.ruoyi.system.service.ISysBusinessService;

import javax.servlet.http.HttpServletRequest;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-03
 */
@Service
public class SysBusinessServiceImpl implements ISysBusinessService 
{
    @Autowired
    private SysBusinessMapper sysBusinessMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysBusiness selectSysBusinessById(String id)
    {
        return sysBusinessMapper.selectSysBusinessById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param sysBusiness 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysBusiness> selectSysBusinessList(SysBusiness sysBusiness)
    {
        return sysBusinessMapper.selectSysBusinessList(sysBusiness);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param sysBusiness 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysBusiness(SysBusiness sysBusiness) throws Exception {
        SysBusiness re = sysBusinessMapper.selectSysBusinessByPhone(sysBusiness.getPhone());
        if(re!=null){

            throw new Exception("手机号码已经注册");
        }


        sysBusiness.setCreateTime(DateUtils.getNowDate());
        sysBusiness.setPassword(Md5Crypt.apr1Crypt(sysBusiness.getPassword(),GlobalConstants.PASSWORD_SALT));
        return sysBusinessMapper.insertSysBusiness(sysBusiness);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param sysBusiness 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysBusiness(SysBusiness sysBusiness)
    {
        sysBusiness.setUpdateTime(DateUtils.getNowDate());
        return sysBusinessMapper.updateSysBusiness(sysBusiness);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessByIds(Long[] ids)
    {
        return sysBusinessMapper.deleteSysBusinessByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessById(Long id)
    {
        return sysBusinessMapper.deleteSysBusinessById(id);
    }

    @Override
    public SysBusiness getUserInfo() {
        String token =  request.getHeader("Authorization");
        if(token!=null && !token.equals("")){
            String userName = TokenUtil.getUserNameFromToken(token);
            String retult = (String) redisTemplate.opsForValue().get("business_"+userName);

            if(retult!=null){
                SysBusiness obj = JSONObject.parseObject(retult,SysBusiness.class);
                return obj;
            }
        }
        return null;
    }

    @Override
    public int editPass(BusinessPasswordParm businessPasswordParm) throws Exception {


        SysBusiness isHas = sysBusinessMapper.selectSysBusinessById(businessPasswordParm.getId());
        if(!isHas.getPhone().equals(businessPasswordParm.getPassword())){
            throw new Exception("密码错误");
        }



        SysBusiness sysBusiness=new SysBusiness();
        sysBusiness.setPassword(businessPasswordParm.getNewPassword());
        sysBusiness.setId(businessPasswordParm.getId());

        return sysBusinessMapper.updateSysBusinessForPhone(sysBusiness);
    }

    @Override
    public SysBusiness Login(BusinessLoginParm businessLoginParm) throws Exception {
        SysBusiness result = sysBusinessMapper.selectSysBusinessByUserName(businessLoginParm.getUserName());

        if(result==null){
            throw new Exception("账号不存在");
        }

        String parPassword=Md5Crypt.apr1Crypt(businessLoginParm.getPassword(), GlobalConstants.PASSWORD_SALT);
        //     sysBusiness.setPassword(Md5Crypt.apr1Crypt(sysBusiness.getPassword(),"54gfdr434vdf43434fd343"));

        if(!parPassword.equals(result.getPassword())){
            throw new Exception("账号密码错误");
        }
        result.setPassword("");
        return result;
    }

}
